//From http://www.quirksmode.org/js/keys.html

var id=0,maxImg=0;

function GoNext(i,of)
{
    var ref;
    var ref0=document.images[i];

    while (1)
    {

    i+=of;
    
    if (i<0)
    {
	ref=document.getElementById('Prev');
	if (!ref) return false;
	location=ref.href+'#last';
	return true;
    } else

    if (i>=maxImg)
    {
	ref=document.getElementById('Next');
	if (!ref) return false;
	location=ref.href;
	return true;
    }

    ref=document.images[i];
    if (!ref) {if (of) continue;else break;}
    
    if (of && (ref.y == ref0.y)) continue;
        
    id=i; // window.scrollY
    window.scroll(0,ref.y-4);
    return true;
    }
//    alert("No Image:"+i);
}

// KeyCode
// 33 = PgUp
// 34 = PgDown
var enaAlert=0;
var AutoNext=0;
var NextTime=5000;
var SkipInterval=false;

function DoSet(){if (SkipInterval) {SkipInterval=false;return 0;}if (AutoNext&1) GoNext(id,+1);}

function DetectPage(e)
{
    var evt = e || window.event;
    if (evt.keyCode==34 || evt.keyCode==33)//PgUp/PgDown
    {
	GoNext(id,(evt.keyCode==34?+1:-1));
	SkipInterval=true;
    }    
    else if (evt.keyCode==36)//Home
    {
	GoNext(0,0);
	SkipInterval=true;
    }
    else if (evt.keyCode==35)//End
    {
	GoNext(maxImg-1,0);
	SkipInterval=true;
    }
    else if (evt.keyCode==170)//Search
    {
	location='/cgi-bin/wall2.cgi';
    }
    else if (evt.keyCode==113)//F2
    {
	enaAlert++;
	if (enaAlert&1) alert("Alert Enabled!");
    }
    else if (evt.keyCode==115)//F4
    {
	AutoNext++;
	SetCookie("SlideShow",AutoNext);
    }
    else if (evt.keyCode==120)//F9
    {
	Params();
    }
    else {
     if (enaAlert&1) alert("KeyCode: "+evt.keyCode);
    return true;
    }
    return false;
}

window.onload = function () {
    document['onkeypress'] = DetectPage;
    var i,ii,x = document.getElementsByTagName('img');
    maxImg=x.length;
    for (i=0;i<maxImg;i++)
    {
	for (ii=i+1;ii<maxImg;ii++)
	{
	    if (x[ii].y != x[i].y) break;
	}
	x[i].next=ii;
	x[i].onclick = function () {SkipInterval=true;GoNext(this.next,0);};
//	x[i].src=urls[i];
    }

//  if (location.hash == '#last') id=maxImg-1; else
    id=maxImg-1;
    if (!window.scrollY) id=0;
    else
    for (i=0;i<maxImg;i++)
    {
	ii=x[i].y-window.scrollY;	
	if (0 <= ii && window.innerHeight > ii)
	{
	    id=i;
	    break;
	}
    }
    LoadCookies();
//    if (!window.scrollY && !location.hash)
    GoNext(id,0);
    if (typeof Init == "function") Init();//"undefined"
//    fullscreen_image();
}

function LoadCookies()
{
    AutoNext=parseInt(GetCookie("SlideShow",AutoNext));
    NextTime=parseInt(GetCookie("NextTime",NextTime));
// SkipInterval=parseInt(GetCookie("SI",SkipInterval));
//    DoSet(NextTime);
    setInterval("DoSet()",NextTime);
}

function Params() {
var tmp=AutoNext;
AutoNext=0;
//SetCookie("SlideShow",AutoNext);
var res=window.showModalDialog("/ParamsDialog.html");
AutoNext=tmp;
}

/*
    $num++;
    my $scr=$num<$a2?
        "window.scroll(0,document.images[$num].y-4);":
#       "window.scroll(0,this.y+this.innerHeight);":
        "location=document.getElementById('Next').href;";
    print qq{<tr><td><a name=$iid></a><img name="i$aid" onclick="$scr"
*/
